
<div class='content-body '>
    <div id='accordion-taux' class='cojean-accordion'>
	<?php  foreach ($tabTaux as $code => $list_taux) : ?>
	    <div class='onglet onglet-close'>
		<h5 class='head head-<?php echo $code;?>'> <?php printf("%s", $manager->getPosteLibelle($code)); ?></h5>
		<div class='body'>
		    <table class='table table-striped  list-table table-manage-taux'>
			<thead>
			    <td width='26%'>Taux horaire</td>
			    <td width='26%'>Nouvelle valeur</td>
			    <td width='26%'>Date d' application</td>            
			    <td width='22%'></td>            
			</thead>


			<tbody>
				<?php foreach ($list_taux as $taux) : ?>
                                    <?php $isAmong = $taux['taux_process'] ;?>   
                            
				    <tr data-store-code='<?php printf($code); ?>' data-store-taux='<?php printf($taux['val']); ?>' data-store-new-taux='' data-store-new-date='' >
					<td ><?php printf($taux['val']); ?></td>									
                                        <td>
                                            <?php if($isAmong ) :  ?>
                                                 <a class='new-taux-editing' data-pk='<?php echo $taux['taux_process']->id ;?>' data-name='nouveau_taux' type='text'  >
                                                    <?php echo $taux['taux_process']->nouveau_taux ; ?>
                                                 </a> 
                                            <?php else : ?>
                                                <a class='new-taux-adding' type='text' data-name='nouveau_taux' >
                                                    
                                                 </a> 
                                            <?php endif; ?>
                                                                                     
                                        </td>
					<td>
                                            <?php if($isAmong ) :  ?>
                                                 <a class='new-taux-date_apply-editing' data-pk='<?php echo $taux['taux_process']->id ;?>' data-name='date_application' data-type="combodate" data-format= "DD/MM/YYYY" data-template= 'D / MMMM / YYYY' data-store=''>
                                                <?php 
                                                   $dateApply	 = new \DateTime($taux['taux_process']->date_application) ;
                                                   $sDate = $dateApply->format('d/m/Y');                                                    
                                                   echo $sDate;
                                                ?>                                              
                                            </a>
                                            <?php else : ?>
                                                <a class='new-taux-date_apply-adding' data-name='date_application' data-type="combodate" data-format= "DD/MM/YYYY" data-template= 'D / MMMM / YYYY' data-store=''>
                                                </a>
                                            <?php endif; ?>
                                            </td>
					<td >    
                                            <span class='ajax-loader'  style='margin-left: 10px; display: none;'> <img src='<?php echo $this->basePath();?>/img/rhx/ajax-waiting.gif' width='25' height='25'></span>
                                            <button class="btn btn-primary btn-small btn-save-proc-taux"      style="display: <?php echo !$isAmong ? '' : 'none' ;?> ;">Enregistrer</button> 
                                            <!--button class="btn btn-inverse btn-waiting-proc-taux"   style="display: <?php echo $isAmong ? '' : 'none' ;?> ;">En attente</button-->
                                            
                                            <div style="display: <?php echo $isAmong ? '' : 'none' ;?> ;">
                                                <span class='ajax-loader'  style='margin-left: 10px; display: none;'> <img src='<?php echo $this->basePath();?>/img/rhx/ajax-waiting.gif' width='25' height='25'></span>
                                                <a href='#' class='btn btn-success btn-small btn-apply-process' data-pk='<?php echo $taux['taux_process']->id ;?>'>Créer les avenants</a>                                            
                                                    &nbsp;&nbsp;&nbsp;
                                                <a class='del-taux' data-pk='<?php echo $taux['taux_process']->id ;?>' href='<?php echo $this->url('RHX/default', array('controller'	 => 'taux', 'action'	 => 'erase')) ?>'>
                                                    <img src='<?php echo $this->basePath() . '/img/rhx/delete-16x16.png' ?>' alt='Effacer' title='Effacer'>
                                                </a>
                                            </div>
                                            
                                        </td>
				    </tr>    
				<?php endforeach; ?>

			</tbody>
		    </table>
		</div>
	    </div>
	<?php endforeach; ?>
    </div>
</div>


<script type="text/javascript">
 (function($)
    {
	jQuery.fn.cojeanAccordion = function(paramCall)
	{  
	    var defautsParam=
	       {
                   "onglet" : null,
		   "param1": 123,
		   "param2": 456,
		   "callback" : null
	       };  
	   return this.each(function(){               
		   var $this = $(this)
			,param=$.extend(defautsParam, paramCall)
			; 
		   
		   $allOnglets = $this.find('.onglet');
		   $allOnglets.removeClass('onglet-open').removeClass('onglet-close').addClass('onglet-close')
				.find('.body').hide();
		   
		   $allOnglets.each( function(){
		       var $thisOnglet = $(this);
		       
		       $thisOnglet.find('.head').on('click', function(){			   
			   if($(this).parent().hasClass('onglet-close')){ 
                                $this.find('.onglet-open').removeClass('onglet-open').addClass('onglet-close')
                                        .find('.body').hide() ;
                                $thisOnglet.removeClass('onglet-close').addClass('onglet-open')
                                        .find('.body').toggle('slide', { direction: "up" }, 200);
                            }
			   
		       });
		       
		   });
		   
		   if(param.callback){
		       param.callback();
		   }
		   // console.log(param);
	       });
	};    

    })(jQuery);
   
</script>



<script type="text/javascript">
    // init accordion
    $("#accordion-taux").cojeanAccordion({
        callback : function(){
            <?php if($onglet) : ?>
                $('.head-<?php echo $onglet; ?>').click(); // :)
            <?php endif; ?>    
        }
    });
    
    // init editable
    var JsTauxLoad = (function(){
       var isFloat = function(val){
                        v = $.trim(val);
                        patFloat = new RegExp(/^[0-9]*[.]{0,1}[0-9]+$/);

                        return patFloat.test(v);                     
                    } ,
           isEmptyAttr = function($obj, attr){
                return $.trim($obj.attr(attr)) === '';
            },
          update_store  = function ($obj, attr_name, v){              
              var v_trimed = $.trim(v)
              $obj.attr(attr_name, v_trimed );
          };
                    
        return {
            init: function(){
                JsTauxLoad.initEditable();                
                JsTauxLoad.saving();
                JsTauxLoad.applying();
            },
            initEditable: function(){
                $('.new-taux-adding').editable({
                    title : 'Nouveau taux',        
                    validate : function (value){                        
                        value = $.trim(value);

                        if(! isFloat(value)){
                            return 'Données invalides.'
                        }                        
                        update_store(  $(this).parent().parent(), 'data-store-new-taux', value);                        
                    }
                    });
                  
                $('.new-taux-date_apply-adding').editable({
                    title : "Date d' application",  
                    url : "<?php echo $this->url('RHX/default', array('controller'	 => 'taux', 'action'	 => 'saveStringData')) ?>",
                    combodate : {
                        minYear: <?php echo (date("Y")); ?>,
                        maxYear: 2025,
                        minuteStep: 1
                    },
                    params: function(params) {
                            value = params.value;
                            date = moment(value, ['YYYY-MM-DD', 'DD/MM/YYYY']);
                            params.value = date.format('YYYY-MM-DD');
                            return params;
                        },
                    validate : function(value){
                        date_apply = value.year() + '-' +  (parseInt(value.month()) + 1)+ '-'+ value.date();
                        update_store(  $(this).parent().parent(), 'data-store-new-date', date_apply);
                    }
                    });  
                     
                 $('.new-taux-editing').editable({
                    title : 'Nouveau taux',
                    url : "<?php echo $this->url('RHX/default', array('controller'	 => 'taux', 'action'	 => 'saveNumberData')) ?>",
                    validate : function (value){                        
                        value = $.trim(value);

                        if(! isFloat(value)){
                            return 'Données invalides.';
                        }
                    }
                    });
                 $('.new-taux-date_apply-editing').editable({
                    title : "Date d' application",  
                    url : "<?php echo $this->url('RHX/default', array('controller'	 => 'taux', 'action'	 => 'saveStringData')) ?>",
                    combodate : {
                        minYear: <?php echo (date("Y")); ?>,
                        maxYear: 2025,
                        minuteStep: 1
                    },
                    params: function(params) {
                            value = params.value;
                            date = moment(value, ['YYYY-MM-DD', 'DD/MM/YYYY']);
                            params.value = date.format('YYYY-MM-DD');
                            return params;
                        }
                    });    
                },
                saving : function(){
                    $('.btn-save-proc-taux').on('click', function(){      
                        var $thisBtn = $(this),
                            $tr = $thisBtn.parent().parent();
                        
                        if(isEmptyAttr($tr, 'data-store-new-taux')){
                            alert('Vous devez fournir la valeur du nouveau taux');
                            return false;
                        }
                        if(isEmptyAttr($tr, 'data-store-new-date')){
                            alert("Vous devez selectionner une date d'application");
                            return false;
                        }                        
                        code = $tr.attr('data-store-code');
                        taux = $tr.attr('data-store-taux');
                        new_taux = $tr.attr('data-store-new-taux');
                        new_date = $tr.attr('data-store-new-date');
                        
                        _data  = 'poste_code='+ code;
                        _data += '&taux='+ taux;
                        _data += '&nouveau_taux='+ new_taux;
                        _data += '&date_application='+ new_date;                        
                        
                        console.log(_data);
                        isValid = true;
                        $thisBtn.toggle();
                        $ajaxLoader = $thisBtn.parent().find('.ajax-loader');
                        $ajaxLoader.toggle();
                        if(isValid){                            
                            addAjax = $.ajax({
                                type : 'post',
                                url : '<?php echo $this->url('RHX/default', array('controller'	 => 'taux', 'action'	 => 'addTaux')) ?>',
                                data : _data                                
                            });
                            addAjax.done (function(data){ 
                                    console.log(data);
                                    if(data.success){                                        
                                        //$tr.find('.btn-waiting-proc-taux').toggle().css('cursor', 'inherit');
                                        //$ajaxLoader.toggle();
                                        JsTaux.refresh({'onglet': code});
                                    }
                                    else{
                                        showError('Erreur Serveur');
                                
                                        $thisBtn.toggle();
                                        $ajaxLoader.toggle();
                                        JsTaux.refresh();
                                    }
                                });
                            addAjax.fail (function(data){
                                showError('Erreur Serveur');
                                
                                $thisBtn.toggle();
                                $ajaxLoader.toggle();
                            });
                        }  
                    });
                },
                applying : function(){
                    $(".btn-apply-process").on('click', function (e){	
                        var $thisBtn = $(this);
                        e.preventDefault();

                        rep = confirm ('Voulez-vous créer les avenants pour les employé concernés!');

                        if(!rep){
                            return 0;
                        }
                        // else

                        pk = $(this).data('pk');

                        $ajaxLoader = $thisBtn.parent().find('.ajax-loader');
                        $delBtn = $thisBtn.parent().find('.del-taux');

                        $thisBtn.toggle();
                        $delBtn.toggle();
                        $ajaxLoader.toggle();
                        $ajax = $.ajax({
                            method : 'post',
                            url : "<?php echo $this->url('RHX/default', array('controller'	 => 'taux', 'action'	 => 'apply')) ?>",
                            data : 'pk='+pk
                        });
                        $ajax.done(function(){
                             $tr = $thisBtn.parent().parent().parent();
                             code = $tr.attr('data-store-code');
                             console.log(code);
                             JsTaux.refresh({'onglet': code});
                        })
                        $ajax.fail(function(){
                            $thisBtn.toggle();
                            $delBtn.toggle();
                            $ajaxLoader.toggle();
                        });
                    }); 
                
                }
        }
        
    } )();
   
    JsTauxLoad.init(); 
    
       
</script>



<style type="text/css">
    
.cojean-accordion .onglet{
    margin : 4px 0;    
    
}
  
.cojean-accordion .onglet .head{
    background: #ededed;
    border-radius: 5px 5px 0 0;
    color: #777777;
    height: 25px;
    padding: 2px 10px;    
    cursor: pointer;
}

.cojean-accordion .onglet .head:hover{
    background-color: #DBDBDB;  
}
  
.cojean-accordion .onglet-close{
    
}
.cojean-accordion .onglet-open{
    margin-bottom: 20px;
}

.cojean-accordion .onglet-close .head{
    
}
.cojean-accordion .onglet-open .head{
    background-color: #DBDBDB;
}

    </style>